// 总条数
let total = 0
// 总页数
let totalpage = 0
// 当前页面数据列表
let list = []

let pageInfo = {
    productName: '',
    page: '1',
    pageSize: '5',
    isPayment:'',
    providerId : ''
}
$(function () {
    getProviderTable()
    getAjax()
})

async function getAjax() {
    let options = {
        url: 'http://146.56.230.35:3007/bill/getBillList',
        type: 'GET',
        data: pageInfo
    }
    let res = await request(options)
    if (res.code !== 200) {
        return alert(res.msg)
    }
    total = res.data.count
    list = res.data.list
    totalpage = Math.ceil(total / pageInfo.pageSize)
    return list
}



async function getProviderTable() {
    try {
        arr = await getAjax()
    } catch (error) {
        if (!arr) {
            return
        }
    }
    let table = $('.providerTable')
    //找到不是第一行的tr
    let tr = table.find('tr:not(:first)')
    //删除tr
    tr.remove()
    if (arr.length === 0) {
        table.append(`<tr><td colspan="7">没有数据</td></tr>`)
        return
    }
    for (let i = 0; i < arr.length; i++) {
        let pay
        if (arr[i].isPayment === 1) {
            pay = '已付款'
        } else {
            pay = '未付款'
        }
        let ste = `
        <tr>
            <td>${arr[i].billCode}</td>
            <td>${arr[i].productName}</td>
            <td>${arr[i].proName}</td>
            <td>￥: ${arr[i].productCount}</td>
            <td>${pay}</td>
            <td>${arr[i].createdAt}</td>
            <td>
                <a href="billView.html?id=${arr[i].id}"><img src="img/read.png" alt="查看" title="查看"/></a>
                <a href="billUpdate.html?id=${arr[i].id}"><img src="img/xiugai.png" alt="修改" title="修改"/></a>
                <a href="##" data-id="${arr[i].id}" class="removeBill"><img src="img/schu.png" alt="删除" title="删除"/></a>
            </td>
        </tr>
        `
        table.append(ste)

    }
    $('.total-page').html(`共${totalpage}页/第${pageInfo.page}页`)
    $('.input-page').val(pageInfo.page)

    // 删除操作
    $('.removeBill').click(async function () {
        let flag = confirm('确定要永久删除吗?')
        console.log(flag);
        if (!flag) {
            return alert('你取消了删除')
        }
        let id = $(this).attr('data-id')
        //准备参数
        let options = {
            url: 'http://146.56.230.35:3007/bill/deleteBillById',
            type: 'DELETE',
            data: {
                id: parseInt(id)
            },
        }
        //执行删除请求
        let res = await request(options)
        console.log(res);
        //删除不成功
        if (res.code !== 200) {
            return alert(res.msg)
        }
        //删除成功，重新加载数据
        getProviderTable()
    })
}

$(function () {
    getAjax()
    //首页
    $('.first-page').click(() => {
        pageInfo.page = 1
        getProviderTable()
    })
    //尾页
    $('.last-page').click(() => {
        pageInfo.page = totalpage
        getProviderTable()
    })
    //上一页
    $('.prev-page').click(() => {
        let num = pageInfo.page
        num--
        if (num <= 0) {
            num = 1
        }
        pageInfo.page = num
        getProviderTable()
    })
    //下一页
    $('.next-page').click(() => {
        let num = pageInfo.page
        num++
        if (num >= totalpage) {
            num = totalpage
        }
        pageInfo.page = num
        getProviderTable()
    })
    //input框回车时触发的操作
    $('.input-page-box').keyup(function (e) {
        let v = this.value
        console.log(v)
        let reg = /^\d+$/
        if (!reg.test(v) || parseInt(v) === 0) {
            this.value = 1
            return
        }
        if (e.which === 13) {
            pageInfo.page = parseInt(v)
            getProviderTable()
        }
    })
    //pagesize发生变化执行的函数
    $('#pagesize').change(function () {
        let size = parseInt(this.value)
        pageInfo.pageSize = size
        getProviderTable()
    })
    //点击查询按钮 按照指定商品名模糊查询用户列表
    $('#searchBtn').click(async function () {
        let v = $('.isPayment').val()
        if(v==='1'){
            v=2
        }else if(v==='2'){
            v=1
        }
        pageInfo.isPayment = v
        let productName = $('.user').val()
        pageInfo.productName = productName
        getProviderTable()
    })
    //搜索框获得焦点的时候清空内容
    $('.user').focus(function () {
        this.value = ''
    })
})
